package com.contacts.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.contacts.pojo.Contact;
import com.contacts.service.ContactService;

public class FindContactByIdServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id=request.getParameter("id");
		try {
			ContactService contactService=new ContactService();
			Contact contact = contactService.findContactById(id);
			if(contact!=null){
				request.setAttribute("contact", contact);
				request.getRequestDispatcher("/updatecontact.jsp").forward(request, response);
				return ;
			}
			
		} catch (Exception e) {
			request.setAttribute("msg", "服务器出错");
			request.getRequestDispatcher("/findall").forward(request, response);
			e.printStackTrace();
		} 
		
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
